<?php

class ExportComponent extends Component {
    static $excel_path = '/PEAR/Spreadsheet/Excel/';
    //static $temp_path = '/var/www/vhosts/cookex.me/beta/vendors/PEAR/';
    static $temp_path = '';
    const INDEX_COLOR_BLACK = 1;
    const INDEX_COLOR_RED = 2;
    const INDEX_COLOR_TITLE = 3;
    const INDEX_COLOR_HEADER = 4;
    const INDEX_COLOR_BG_HEADER = 5;
    const INDEX_COLOR_BODER = 6;
    //const NAME = 'Name';
    public static function datei_mime($filetype) {
        switch ($filetype) {
            case "ez":  $mime="application/andrew-inset"; break;
            case "hqx": $mime="application/mac-binhex40"; break;
            case "cpt": $mime="application/mac-compactpro"; break;
            case "doc": $mime="application/msword"; break;
            case "bin": $mime="application/octet-stream"; break;
            case "dms": $mime="application/octet-stream"; break;
            case "lha": $mime="application/octet-stream"; break;
            case "lzh": $mime="application/octet-stream"; break;
            case "exe": $mime="application/octet-stream"; break;
            case "class": $mime="application/octet-stream"; break;
            case "dll": $mime="application/octet-stream"; break;
            case "oda": $mime="application/oda"; break;
            case "pdf": $mime="application/pdf"; break;
            case "ai":  $mime="application/postscript"; break;
            case "eps": $mime="application/postscript"; break;
            case "ps":  $mime="application/postscript"; break;
            case "xls": $mime="application/vnd.ms-excel"; break;
            case "ppt": $mime="application/vnd.ms-powerpoint"; break;
            case "wbxml": $mime="application/vnd.wap.wbxml"; break;
            case "wmlc": $mime="application/vnd.wap.wmlc"; break;
            case "wmlsc": $mime="application/vnd.wap.wmlscriptc"; break;
            case "vcd": $mime="application/x-cdlink"; break;
            case "pgn": $mime="application/x-chess-pgn"; break;
            case "csh": $mime="application/x-csh"; break;
            case "dvi": $mime="application/x-dvi"; break;
            case "spl": $mime="application/x-futuresplash"; break;
            case "gtar": $mime="application/x-gtar"; break;
            case "hdf": $mime="application/x-hdf"; break;
            case "js":  $mime="application/x-javascript"; break;
            case "nc":  $mime="application/x-netcdf"; break;
            case "cdf": $mime="application/x-netcdf"; break;
            case "swf": $mime="application/x-shockwave-flash"; break;
            case "tar": $mime="application/x-tar"; break;
            case "tcl": $mime="application/x-tcl"; break;
            case "tex": $mime="application/x-tex"; break;
            case "texinfo": $mime="application/x-texinfo"; break;
            case "texi": $mime="application/x-texinfo"; break;
            case "t":   $mime="application/x-troff"; break;
            case "tr":  $mime="application/x-troff"; break;
            case "roff": $mime="application/x-troff"; break;
            case "man": $mime="application/x-troff-man"; break;
            case "me":  $mime="application/x-troff-me"; break;
            case "ms":  $mime="application/x-troff-ms"; break;
            case "ustar": $mime="application/x-ustar"; break;
            case "src": $mime="application/x-wais-source"; break;
            case "zip": $mime="application/zip"; break;
            case "au":  $mime="audio/basic"; break;
            case "snd": $mime="audio/basic"; break;
            case "mid": $mime="audio/midi"; break;
            case "midi": $mime="audio/midi"; break;
            case "kar": $mime="audio/midi"; break;
            case "mpga": $mime="audio/mpeg"; break;
            case "mp2": $mime="audio/mpeg"; break;
            case "mp3": $mime="audio/mpeg"; break;
            case "aif": $mime="audio/x-aiff"; break;
            case "aiff": $mime="audio/x-aiff"; break;
            case "aifc": $mime="audio/x-aiff"; break;
            case "m3u": $mime="audio/x-mpegurl"; break;
            case "ram": $mime="audio/x-pn-realaudio"; break;
            case "rm":  $mime="audio/x-pn-realaudio"; break;
            case "rpm": $mime="audio/x-pn-realaudio-plugin"; break;
            case "ra":  $mime="audio/x-realaudio"; break;
            case "wav": $mime="audio/x-wav"; break;
            case "pdb": $mime="chemical/x-pdb"; break;
            case "xyz": $mime="chemical/x-xyz"; break;
            case "bmp": $mime="image/bmp"; break;
            case "gif": $mime="image/gif"; break;
            case "ief": $mime="image/ief"; break;
            case "jpeg": $mime="image/jpeg"; break;
            case "jpg": $mime="image/jpeg"; break;
            case "jpe": $mime="image/jpeg"; break;
            case "png": $mime="image/png"; break;
            case "tiff": $mime="image/tiff"; break;
            case "tif": $mime="image/tiff"; break;
            case "wbmp": $mime="image/vnd.wap.wbmp"; break;
            case "ras": $mime="image/x-cmu-raster"; break;
            case "pnm": $mime="image/x-portable-anymap"; break;
            case "pbm": $mime="image/x-portable-bitmap"; break;
            case "pgm": $mime="image/x-portable-graymap"; break;
            case "ppm": $mime="image/x-portable-pixmap"; break;
            case "rgb": $mime="image/x-rgb"; break;
            case "xbm": $mime="image/x-xbitmap"; break;
            case "xpm": $mime="image/x-xpixmap"; break;
            case "xwd": $mime="image/x-xwindowdump"; break;
            case "msh": $mime="model/mesh"; break;
            case "mesh": $mime="model/mesh"; break;
            case "silo": $mime="model/mesh"; break;
            case "wrl": $mime="model/vrml"; break;
            case "vrml": $mime="model/vrml"; break;
            case "css": $mime="text/css"; break;
            case "asc": $mime="text/plain"; break;
            case "txt": $mime="text/plain"; break;
            case "gpg": $mime="text/plain"; break;
            case "rtx": $mime="text/richtext"; break;
            case "rtf": $mime="text/rtf"; break;
            case "wml": $mime="text/vnd.wap.wml"; break;
            case "wmls": $mime="text/vnd.wap.wmlscript"; break;
            case "etx": $mime="text/x-setext"; break;
            case "xsl": $mime="text/xml"; break;
            case "flv": $mime="video/x-flv"; break;
            case "mpeg": $mime="video/mpeg"; break;
            case "mpg": $mime="video/mpeg"; break;
            case "mpe": $mime="video/mpeg"; break;
            case "qt":  $mime="video/quicktime"; break;
            case "mov": $mime="video/quicktime"; break;
            case "mxu": $mime="video/vnd.mpegurl"; break;
            case "avi": $mime="video/x-msvideo"; break;
            case "movie": $mime="video/x-sgi-movie"; break;
            case "asf": $mime="video/x-ms-asf"; break;
            case "asx": $mime="video/x-ms-asf"; break;
            case "wm":  $mime="video/x-ms-wm"; break;
            case "wmv": $mime="video/x-ms-wmv"; break;
            case "wvx": $mime="video/x-ms-wvx"; break;
            case "ice": $mime="x-conference/x-cooltalk"; break;
            case "rar": $mime="application/x-rar"; break;
            default:    $mime="application/octet-stream"; break; 
        }
        return $mime;
    }
    public function save($file)
    {
	$filename = basename($file);
        $file_extension = strtolower(substr(strrchr($filename,"."),1));
        $ctype = self::datei_mime($file_extension);
        $len = filesize($file);
        header("Cache-Control: public, must-revalidate");
        header('Cache-Control: pre-check=0, post-check=0, max-age=0');
        header("Pragma: no-cache");
        header("Content-type: application/vnd.ms-excel; charset=UTF-8; encoding=UTF-8");
        header("Expires: 0"); 
        header("Content-Description: File Transfer");
        header("Content-Type: " . $ctype);
        header("Content-Length: ".(string)$len);
        header('Content-Disposition: attachment; filename="'.$filename.'"');
        header("Content-Transfer-Encoding: binary\n");
        echo @file_get_contents($file);
    }
    public function header(&$workbook, &$worksheet,$title, $arr_header = array(), $title_table = '')
    {
        $format_title = & $workbook->addFormat();
        $format_title->setFontFamily('Arial');
        $format_title->setSize(18);

        $format_title->setBorderColor("black");
        $format_title->setTop(1);
        $format_title->setLeft(1);
        $format_title->setRight(1);
        $format_title->setBottom(1);
        $format_title->setBold();
        $format_title->setHAlign("center");
        $format_title->setBold();
        $format_title->setTextWrap();

        $format_title_header = & $workbook->addFormat();
        $format_title_header->setFontFamily('Arial');
        $format_title_header->setSize(15);
        $format_title_header->setBorderColor("black");
        $format_title_header->setTop(1);
        $format_title_header->setLeft(1);
        $format_title_header->setRight(1);
        $format_title_header->setBottom(1);
        $format_title_header->setBold();
        $format_title_header->setTextWrap();
        $format_title_header->setHAlign("center");
        $worksheet->setInputEncoding("UTF-8");
		
        $row=0;
        //$worksheet->setRow(0,70);
        //$worksheet->setColumn(0,0,50); // row 0 col 0
        //$worksheet->setColumn(1,1,50); // row 1 col 1 <-- setting row1 and col1 (instead of row0 col1)

       // $worksheet->insertBitmap(0,0,'/logo-export.bmp',0,0,1,1);
        
        //$row=2;
        //if($type=='all'){
        //    $row = $this->image_header_full($workbook,$worksheet);
		//}else{
        //    $row = $this->image_header_short($workbook,$worksheet);
		//}
        $worksheet->write($row,0,$title,$format_title);
        $worksheet->setRow(0,30);
        $worksheet->mergeCells($row,0,$row,count($arr_header)-1);
		//$row = 3;
        $i = 0;
        //print_r($arr_header);die;
        $row ++ ;
        foreach ($arr_header as $name=>$width)
        {
            $worksheet->write($row, $i, $name,$format_title_header);
            $worksheet->setColumn($i, $i, $width);
            $i++;
        }
        return $row+1;
    }
    public function image_header_full(&$workbook, &$worksheet)
    {
        $export_folder = WWW_ROOT.'/files/';
        $format_title_header = & $workbook->addFormat();
        $format_title_header->setHAlign("center");
        $format_title_header->setBorderColor("black");
        $format_title_header->setTop(1);
        $format_title_header->setLeft(1);
        $format_title_header->setRight(1);
        $format_title_header->setBottom(1);
        $worksheet->write(0,0,' ',$format_title_header);
        $worksheet->insertBitmap(0, 3, $export_folder.'logo-export.bmp', 1, 1, 0.5, 1);
        $worksheet->mergeCells(0,0,6,6);
        $worksheet->write(7,0,'Email : report@cookex.me',$format_title_header);
        $worksheet->mergeCells(7,0,7,6);
        $worksheet->mergeCells(0,0,7,6);
        return 8;
    }
    public function image_header_short(&$workbook, &$worksheet)
    {
        $export_folder = WWW_ROOT.'/files/';
        $format_title_header = & $workbook->addFormat();
        $format_title_header->setHAlign("center");
        $format_title_header->setBorderColor("black");
        $format_title_header->setTop(1);
        $format_title_header->setLeft(1);
        $format_title_header->setRight(1);
        $format_title_header->setBottom(1);
        $worksheet->write(0,0,' ',$format_title_header);
        $worksheet->insertBitmap(0, 1, $export_folder.'logo-export.bmp', 1, 1, 0.4, 1);
        $worksheet->mergeCells(0,0,2,2);
        $worksheet->write(7,0,'Email : report@cookex.me',$format_title_header);
        $worksheet->mergeCells(7,0,7,2);
        $worksheet->mergeCells(0,0,7,2);
        return 8;
    }
    public function exportSample()
    {		
        set_include_path(VENDORS.'PEAR/');
        require_once (VENDORS.self::$excel_path.'Writer.php');
        //require_once 'Spreadsheet/Excel/Writer.php';
        $export_folder = WWW_ROOT.'/files/';
        $file = $export_folder.'test.xls';
        // We give the path to our file here
        $workbook = new Spreadsheet_Excel_Writer($file);
        $format_title_header = & $workbook->addFormat();
        $format_title_header->setHAlign("center");
        $worksheet =& $workbook->addWorksheet('My first worksheet');
        
        $worksheet->insertBitmap(0, 2, $export_folder.'logo-export.bmp', 0, 4, 1, 1);
        $worksheet->mergeCells(0,0,6,6);
        $worksheet->write(7,0,'Email : report@cookex.me',$format_title_header);
        $worksheet->mergeCells(7,0,7,6);
        $worksheet->mergeCells(0,0,7,6);
        $workbook->close();
        $this->save($file);
    }
    public function export_selling_list($title,$sells){
        set_include_path(VENDORS.'PEAR/');
        require_once (VENDORS.self::$excel_path.'Writer.php');
        $export_folder = WWW_ROOT.'/files/';
        $file =$export_folder."$title.xls";
        if (file_exists($file))
            unlink($file);
        $workbook = new Spreadsheet_Excel_Writer($file);
        $workbook->setTempDir(self::$temp_path);
        $format_content_boder = & $workbook->addFormat();
        $format_content_boder->setFontFamily('Arial');
        $format_content_boder->setSize(12);
        $format_content_boder->setBorderColor("black");
        $format_content_boder->setTop(1);
        $format_content_boder->setLeft(1);
        $format_content_boder->setRight(1);
        $format_content_boder->setBottom(1);
        $arr_header = array();
        $arr_header = array('No.'=>5, 'Code'=>30, 'Type'=>20,'Amount'=>20,'Time'=>30);
        $worksheet =& $workbook->addWorksheet('Selling');
        $row = $this->header($workbook,$worksheet, $title, $arr_header);
        $i=1;
        foreach ($sells as $sell):
            $worksheet->write($row, 0, $i++ ,$format_content_boder);
            $worksheet->writeString($row, 1, $sell['CashierSell']['code'],$format_content_boder );
            $worksheet->writeString($row, 2, $sell['CashierSell']['type'],$format_content_boder );
            $worksheet->write($row, 3, $sell['CashierSell']['amount'],$format_content_boder );
            $worksheet->writeString($row, 4, $sell['CashierSell']['created'],$format_content_boder );
            $row++;
        endforeach; 
        $workbook->close();
        $this->save($file);
    }
    function export_order($orders,$type,$date){
        //$this->exportSample();
        set_include_path(VENDORS.'PEAR/');
        require_once (VENDORS.self::$excel_path.'Writer.php');
        if($type=='all'){
            $title = '';
            if($date){
                $date = explode("-",$date);
                $date = $date[2].'_'.$date[1].'_'.$date[0];
                $title = 'Orders List date '.$date;
            }else{
                $title = 'Orders List';
            }
            $export_folder = WWW_ROOT.'/files/';
    		$file =$export_folder."$title.xls";
    		if (file_exists($file))
    			unlink($file);
            
            $workbook = new Spreadsheet_Excel_Writer($file);
            $workbook->setTempDir(self::$temp_path);
            $format_content_boder = & $workbook->addFormat();
    		$format_content_boder->setFontFamily('Arial');
    		$format_content_boder->setSize(12);
    		$format_content_boder->setBorderColor("black");
    		$format_content_boder->setTop(1);
    		$format_content_boder->setLeft(1);
    		$format_content_boder->setRight(1);
    		$format_content_boder->setBottom(1);
            $arr_header = array();
    	    $arr_header = array('No.'=>5, 'Order Id'=>13, 'Name'=>30,'Mobile'=>20,'Payment Type'=>20,'Created Time'=>20,'Total'=>20);
    		$worksheet =& $workbook->addWorksheet('Order List');
    		$row = $this->header($type,$workbook,$worksheet, $title, $arr_header);
            $i=1;
            foreach ($orders as $order):
                $worksheet->write($row, 0, $i++ ,$format_content_boder);
                $worksheet->write($row, 1, $order['Order']['id'],$format_content_boder );
    			$worksheet->writeString($row, 2, $order['Order']['first_name'].' '.$order['Order']['surname'],$format_content_boder );
    			$worksheet->writeString($row, 3, $order['Order']['mobile'],$format_content_boder );
                $worksheet->writeString($row, 4, $order['Order']['payment_method'],$format_content_boder );
                $dt = explode(" ",$order['Order']['created']);
                $orddate = explode("-",$dt[0]);
                $orddtsh = $orddate[2].'-'.$orddate[1].'-'.$orddate[0];
                $ordtime = explode(":",$dt[1]);
                $ordtimesh = $ordtime[0].':'.$ordtime[1];
                $worksheet->writeString($row, 5, $orddtsh." ".$ordtimesh,$format_content_boder );
                $worksheet->write($row, 6, $order['Order']['payment_total'],$format_content_boder );
                $row++;
            endforeach; 
		}elseif($type == 'emails'){
    		$title = '';
            if($date){
                $date = explode("-",$date);
                $date = $date[2].'_'.$date[1].'_'.$date[0];
                $title = 'Emails List date '.$date;
            }else{
                $title = 'Emails List';
            }
            $export_folder = WWW_ROOT.'/files/';
    		$file =$export_folder."$title.xls";
    		if (file_exists($file))
    			unlink($file);
            $workbook = new Spreadsheet_Excel_Writer($file);
            $workbook->setTempDir(self::$temp_path);
            $format_content_boder = & $workbook->addFormat();
    		$format_content_boder->setFontFamily('Arial');
    		$format_content_boder->setSize(12);
    		$format_content_boder->setBorderColor("black");
    		$format_content_boder->setTop(1);
    		$format_content_boder->setLeft(1);
    		$format_content_boder->setRight(1);
    		$format_content_boder->setBottom(1);
            $arr_header = array();
    		$arr_header = array('No.'=>8, 'Order Id'=>13, 'Email'=>30);
    		$worksheet =& $workbook->addWorksheet('Email List');
    		$row = $this->header($type,$workbook,$worksheet, $title, $arr_header);
            $i=1;
            foreach ($orders as $order):
                $worksheet->write($row, 0, $i++ ,$format_content_boder);
                $worksheet->write($row, 1, $order['Order']['id'] ,$format_content_boder);
    			$worksheet->writeString($row, 2, $order['Order']['email'],$format_content_boder );
    			$row++;
            endforeach; 
		}elseif($type == 'mobile'){
            $title = '';
        
            if($date){
                $date = explode("-",$date);
                $date = $date[2].'_'.$date[1].'_'.$date[0];
                $title = 'Mobile Number List date '.$date;
            }else{
                $title = 'Mobile Number List';
            }
		
            $export_folder = WWW_ROOT.'/files/';
    		$file =$export_folder."$title.xls";
            //echo $file;die;
    		if (file_exists($file))
    			unlink($file);
            
            $workbook = new Spreadsheet_Excel_Writer($file);
            $workbook->setTempDir(self::$temp_path);
            $format_content_boder = & $workbook->addFormat();
    		$format_content_boder->setFontFamily('Arial');
    		$format_content_boder->setSize(12);
    		$format_content_boder->setBorderColor("black");
    		$format_content_boder->setTop(1);
    		$format_content_boder->setLeft(1);
    		$format_content_boder->setRight(1);
    		$format_content_boder->setBottom(1);
            $format_content_boder->setHAlign("center");
            $arr_header = array();
    		$arr_header = array('No.'=>8, 'Order Id'=>13, 'Mobile'=>30);
    		$worksheet =& $workbook->addWorksheet('Mobile List');
            //var_dump($worksheet);
            
    		$row = $this->header($type,$workbook,$worksheet, $title, $arr_header);
            $i=1;
            foreach ($orders as $order):
                $worksheet->write($row, 0, $i++ ,$format_content_boder);
                $worksheet->write($row, 1, $order['Order']['id'] ,$format_content_boder);
    			$worksheet->writeString($row, 2, $order['Order']['mobile'] ,$format_content_boder);
    			$row++;
            endforeach; 
		}
        $workbook->close();
		$this->save($file);
    }      
}